<?php

namespace app\admin\controller;


class Versioncate extends Base
{
    /**
     * app类型栏目显示
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $versioncate = \app\common\model\Versioncate::where('status', '<>', -1)->order('id', 'desc')->paginate(3);
        return $this->fetch('', [
            'versioncate' => $versioncate,
        ]);
    }

    /**
     * app类型栏目添加功能
     * @return mixed|void
     */
    public function add()
    {

        if (request()->isPost()) {
            $data = input('param.');
            $validate = validate('VersionCate');

            if (!$validate->check($data)) {
                return $this->error($validate->getError());
            }

            //入库操作
            try {
                $id = model('Versioncate')->add($data);
            } catch (\Exception $e) {
                return $this->result('', 0, $this->error($e->getMessage()));
            }

            if ($id) {
                return $this->result(['jump_url' => url('versioncate/index')], 1, 'OK');
            } else {
                return $this->result('', 0, '新增失败');
            }
        } else {
            return $this->fetch();
        }
    }


    /**
     * app类型栏目显示修改
     * @return mixed|void
     */
    public function edit()
    {
        if (request()->isPost()) {
            $data = input('param.');
            $post = input('post.');
            $validate = validate('Versioncate');
            if (!$validate->check($data)) {
                return $this->error($validate->getError());
            }

            try {
                $user = model('Versioncate')->get(['id' => $data['id']]);
            } catch (\Exception $e) {
                return $this->result('', 0, $e->getMessage());
            }

            try {
                $res = model('Versioncate')->save($post, ['id' => $data['id']]);
            } catch (\Exception $e) {
                return $this->result('', 0, $e->getMessage());
            }

            if ($res) {
                return $this->result(['jump_url' => url('versioncate/index')], 1, 'OK');
            }
            return $this->result('', 0, '编辑失败');


        } else {
            try {
                $data = input('param.');
                $news = model('Versioncate')->get(['id' => $data['id']]);
            } catch (\Exception $e) {
                return $this->result('', 0, $this->error($e->getMessage()));
            }
            return $this->fetch('', [
                'data' => $news,
            ]);
        }

    }


}
